<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-04-20 10:01:34
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-05-27 18:43:52
*/
//无限级分类处理类
Class Category{
	/**
	 * 组合等级数组
	 * @param  [type]  $cate  [description]
	 * @param  string  $html  [description]
	 * @param  integer $pid   [description]
	 * @param  integer $level [description]
	 * @param  string  $cid   [description]
	 * @return [type]         [description]
	 */
	static function getLevel($cate,$html='└─',$cid='cid',$pid=0,$level=0){
		$arr=array();
		foreach ($cate as $v) {
			if($v['pid']==$pid){
				$v['level'] = $level + 1;
				$htmld=($pid==0)?'':$html;
				$v['html'] = str_repeat('&emsp;&emsp;', $level).$htmld;
				$arr[]=$v;
				$arr=array_merge($arr,self::getLevel($cate,$html,$cid,$v[$cid],$level+1));
			}
		}
		return $arr;
	}
	/**
	 * 获得子类 多维数组
	 * @param  [type]  $cate [description]
	 * @param  integer $pid  [description]
	 * @param  string  $cid   [description]
	 * @return [type]        [description]
	 */
	static function getChilds($cate,$pid=0,$cid='cid'){
		$arr=array();
		foreach ($cate as $v) {
			if($v['pid']==$pid){
				$v['child']=self::getChilds($cate,$v[$cid],$cid);
				$arr[]=$v;
			}
		}
		return $arr;
	}
	/**
	 * 获得所有子类Id
	 * @param  [type] $cate [description]
	 * @param  [type] $id   [description]
	 * @param  string $cid  [description]
	 * @return [type]       [description]
	 */
	static function getChildsId($cate,$id,$cid='cid'){
		$arr=array();
		foreach ($cate as $v) {
			if($v['pid']==$id){
				$arr[]=$v[$cid];
				$arr=array_merge($arr,self::getChildsId($cate,$v[$cid],$cid));
			}
		}
		return $arr;
	}
	/**
	 * 获得所有子类Id包括名称
	 * @param  [type] $cate [description]
	 * @param  [type] $id   [description]
	 * @param  string $cid  [description]
	 * @return [type]       [description]
	 */
	static function getChildsAll($cate,$id,$cid='cid'){
		$arr=array();
		foreach ($cate as $v) {
			if($v['pid']==$id){
				$arr[]=$v;
				$arr=array_merge($arr,self::getChildsAll($cate,$v[$cid],$cid));
			}
		}
		return $arr;
	}
	/**
	 * 查找父分类
	 * @param  [type]  $cate [description]
	 * @param  integer $id   [description]
	 * @param  string  $cid  [description]
	 * @return [type]        [description]
	 */
	static function getParents($cate,$id=0,$cid='cid'){
		$arr=array();
		foreach($cate as $v){
			if($v[$cid]==$id){
				$arr[]=$v;
				$arr=array_merge(self::getParents($cate,$v['pid'],$cid),$arr);
			}
		}
		return $arr;
	}
}
